#include "communication-worker.h"
#include "smart-device-net-protocol.h"

CommunicationWorker::CommunicationWorker(const string &uartDevice)
{
	smartDeviceManager      = new SmartDeviceManager(uartDevice);
	deviceMap["doorlock"]   = new CommunicationDeviceDoorlock(smartDeviceManager);
	deviceMap["electrical"] = new CommunicationDeviceElectrical(smartDeviceManager);
	deviceMap["sensor"]     = new CommunicationDeviceSensor(smartDeviceManager);
	deviceMap["sniffing"]   = new CommunicationDeviceSniffing(smartDeviceManager);
	deviceMap["list"]       = new CommunicationDeviceList();
#if 0
	fingerprintManager       = new FingerPrintManager(new FingerPrintDevice("/dev/ttyUSB0"));
	deviceMap["fingerprint"] = new CommunicationDeviceFingerPrint(fingerprintManager);
#endif
}

void CommunicationWorker::doWork(const CommunicationMessage &messages)
{

	vector<uint8_t> packet = messages.getPacket();
	//获取通讯的对象udp
	NetCommunication *netObj = messages.getNetCommuicationObject();

	string                 jsonData(packet.data(), packet.data() + packet.size());
	SmartDevicenetProtocol protocol = SmartDevicenetProtocol::fromJsonData(jsonData);
	if(protocol.empty())
	{
		fprintf(stderr, "SmartDevicenetProtocol obj is empty\n");
		return;
	}
	//获取操作的类型是门锁，传感器还是，谭旭设备
	string               operation = protocol.getOperation();
	CommunicationDevice *device    = deviceMap[operation];
	if(!device)
	{
		fprintf(stderr, "Invalida operation:%s\n", operation.c_str());
		return;
	}
	//设备中去处理
	device->communication(protocol, netObj);
}
